﻿var kkcai = {
	isShowDS : true,
	isShowDX : true,
	isShowHZ : true,
	isShowRowLine : true,
	border : 0,
	font: 2,
	color : 1,
	omit : 1,
	limit : 37,
	phase : '',
	time : 0,
	uid : 0,
	tid : 0,
	
	init : function(){
		this.limit = parseInt( ($(document).height() - 130) / 29 );

		var h=$(document).height()- this.limit*29-130;
		$("marquee").css("line-height",h+"px").height(h);
	
		this.getConfig();
		
		this.doUpdate();		
	},
	
	getConfig : function (){
	    var val = document.location.href;
	
	    if (val.indexOf("?")>0) {
	        val = val.substr(val.indexOf("?")+1);
	
	        this.isShowDS = val.substr(0,1) == "1";
	        this.isShowDX = val.substr(1,1) == "1";
	        this.isShowHZ = val.substr(2,1) == "1";
	        this.isShowRowLine = val.substr(3,1) == "1";
	        this.color = parseInt(val.substr(4,1));
	        this.font = parseInt(val.substr(5,1));
	        this.omit = parseInt(val.substr(6,1));
	        this.border = parseInt(val.substr(7,1));
	    }
	},
	
	doUpdate : function(){
		$.get("api.aspx", {limit: kkcai.limit, t: Math.random()}, function(json){
	    	var phase = json.record[0].TermNo; 
	    	if(phase === kkcai.phase)
	    		return;
	    	
			kkcai.phase = phase;
			
			clearInterval(kkcai.uid);
			clearInterval(kkcai.tid);
			
	        $("#pan").html(kkcai.getBody(json));
	        
	        kkcai.countDown();
	        
	        kkcai.render();
		
	    }, 'json');
	},
	
	render : function(){
        //前三颜色
        switch(kkcai.color){
        	case 1: break;
        	case 2: $(".d2, .d3").css("color", "black"); break;
        	case 3: $(".d2, .d3").css("color", "red"); break;
        }
		//遗漏值颜色
		switch(kkcai.omit){
        	case 1:	break;
        	case 2: $(".yl").each(function(){$(this).css("color","#CCC")}); break;
        	case 3: $(".yl").each(function(){$(this).css("color","#BFBFBF")}); break;
        }
        //开奖号字体
        switch(kkcai.font){
        	case 1: $(".b, .d1, .d2, .d3").css("font-size","22px"); break;
        	case 2: $(".b, .d1, .d2, .d3").css("font-size","22px"); break;
        	case 3: $(".b, .d1, .d2, .d3").css("font-size","22px"); break;
        }
        //标题颜色
        $("#htbMain tr:first").each(function(){
			$(this).find("th").slice(2,14).css("background-color","red");
			$(this).find("th").slice(14,26).css("background-color","blue");
			$(this).find("th").slice(26,38).css("background-color","green");
		});
    	//边框颜色
        var color = "#000";
        if(kkcai.border == 1){
        	color = "red";
        	$("td").css("border-color", "#000");
        }
        $("#htbMain tr").each(function(){
        	$(this).find("td").eq(1).css("border-right-color", color);
        	$(this).find("td").eq(13).css("border-right-color", color);
        	$(this).find("td").eq(25).css("border-right-color", color);
        	$(this).find("td").eq(37).css("border-right-color", color);
        });
        //10行区分线
		if(kkcai.isShowRowLine){
			$("#htbMain tr").each(function(i){
				if(i%10==0) $(this).find("td").slice(2,38).css("border-bottom-color", color)}
			);
		}
		
		//连接线
		draw.init("#htbMain", { color: "red", width: 1 });
		drawlines($(".d1"), "red");
		drawlines($(".d2"), "blue");
		drawlines($(".d3"), "green");
	},
	
	showTime : function(){
        var time = kkcai.time - 1;
        if(time < 0) return;
        
        if(time > 0){
            var min = Math.floor( time / 60 );
            var sec = time % 60;
            
            if (min < 10) { min = "0" + min;}
			if (sec < 10) { sec = "0" + sec;}
			
            $("#stopTime").text("截止时间 " + min + ":" + sec);
        }else{
        	$("#tip").text("正在开奖");
        	$("#stopTime").text("祝您中奖");
            kkcai.uid = setInterval("kkcai.doUpdate()", 10 * 1000);
        }
        
        kkcai.time = time;
	},
	
	countDown : function(){
		$.get("time.aspx", {limit: kkcai.limit, t: Math.random()}, function(sec){
			kkcai.time = parseInt(sec);
			
			kkcai.tid = setInterval("kkcai.showTime()", 1000);
		});
	},
	
	calcArray : function (json, index) {
		index--;
		
		var arrayObj = new Array(json.length);
		
		var arr = new Array(13);
	    var val = json[json.length-1].OpenNum.split(',')[index];
	 
	    for(var i = 1; i < 13; i++)
	    	arr[i] = 1;
	    arr[parseInt(val, 10)] = 0;
		
		arrayObj[json.length-1] = arr;
	
		for(var row = json.length-2; row >= 0; row--) {
		    arr = new Array(13);
		    val = json[row].OpenNum.split(',')[index];
	
		    for(var i = 1; i < 13; i++)
		    	arr[i] = arrayObj[row+1][i] + 1;
	        arr[parseInt(val, 10)] = 0;
		    arrayObj[row] = arr;
		}
		
		return arrayObj;
	},
	
	getTitleTR : function(){
		var title = ["期号","冠/亚/季"
			,"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"
			,"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"
			,"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"
		];
		
		var tr = document.createElement("tr");
		var td;
		for(var i=0; i<title.length; i++){
			td = document.createElement("th");
			td.innerHTML = title[i];
			tr.appendChild(td);
		}
		
		return tr;
	},
	
	getNextTR : function(num){
		84 == num ? num = 1 : num++;	
		10 > num && (num = "0" + num);
		
		var tr = document.createElement("tr");
		var td;
		
		td = document.createElement("td");
		td.innerHTML = num;
		td.className = "qh";
		tr.appendChild(td);
		
		td = document.createElement("td");
		td.innerHTML = "销售中...";
		td.id = "tip";
		td.style.color="red";
		td.className = "a3";
		tr.appendChild(td);
	
		for (var i = 0; i < 36; i++){
			td = document.createElement("td");
		    tr.appendChild(td);
		}
		/*
		//stop_time
		td = document.createElement("td");
    	td.className = "m";
    	td.id = "stopTime";
    	td.colSpan = 36;
	    tr.appendChild(td);
		*/
		return tr;
	},
	
	getCountTR : function(array){
		var tr = document.createElement("tr");
		
		var td = document.createElement("th");
		td.colSpan = 2;
		td.innerHTML = "今日出现次数";
		tr.appendChild(td);
	
		for(var i=0; i<array.length; i++){
			td = document.createElement("th");
			td.innerHTML = array[i];
			tr.appendChild(td);
		}
		
		td = document.createElement("th");
    	td.colSpan = 24;
	    tr.appendChild(td);
		
		return tr;
	},
	
	getBody : function(json){
		var array = new Array(3);
		for (var i = 0; i < 3; i++) {
			array[i] = this.calcArray(json.record, i+1); 
		}
		
		var tbody = document.createElement("tbody");
		tbody.appendChild( this.getTitleTR() );
		
		var tr, cell;
		for(var row = json.record.length-1; row >= 0; row--) {
			tr = document.createElement("tr");
			
			//phase
			cell = document.createElement("td");
			cell.className = "qh";
			cell.innerHTML = json.record[row].TermNo.substring(6);
			tr.appendChild(cell);
	
			//A3
			cell = document.createElement("td");
			cell.className = "a3";
			cell.innerHTML = json.record[row].OpenNum;//substring(0,8).replace(/[,]/g, ' ');
			tr.appendChild(cell);
			
			//trend
			var tmp;
			
			for (var i = 0; i < 3; i++) {
				for (var col = 1; col <= 12; col++) {
					cell = document.createElement("td");
					//cell.width = '4%'
					cell.innerHTML = array[i][row][col];
					cell.className = "yl";
		
					tmp = (col < 10 ? "0" : "") + col;
					if( json.record[row].OpenNum.split(',')[i] === tmp ) { 
						cell.innerHTML = col;
						cell.className = "d" + (i+1);
					}
					
					tr.appendChild(cell);
				}
			}
			
			tbody.appendChild(tr);
		}
	
		var num = parseInt( json.record[0].TermNo.substring(6), 10 );
		tbody.appendChild( this.getNextTR(num) );	
		//tbody.appendChild( this.getCountTR(json.count) );
		
		var table = document.createElement("table");
		table.id="htbMain";
		table.appendChild(tbody);
		table.width = '100%';
		return table;
	}
};

$(function() {
	kkcai.init();
})